<?php

define('ON_DOC_READY', TRUE);

class BlgJSStack{
	protected $out;	
	protected $in;
	
	function __construct(){
		$this->out= array();
		$this->in= array();
	}
	
	public function append($code, $on_ready= ON_DOC_READY){
		if($on_ready)
			$this->in[]= "\t".rtrim($code);
		else
			$this->out[]= "\t".rtrim($code);
	}
	
	public function code($htmltags= FALSE){	
		$str= '';
		
		if(FALSE!==$htmltags)
			$str= '<script language="javascript">'."\n";	
			
		$str.= implode("\n", $this->out)."\n";
		
		$str.= "$(document).ready(function(){"."\n";
		$str.= implode("\n", $this->in);
		$str.= "\n"."});";
		
		if(FALSE!==$htmltags)
			$str.= "\n\t".'</script>';

		return $str;
	}
	
	public function __toString(){
		echo $this->code();
	} 
}